﻿using Dapper;
using EquipmentModule.Contracts;
using EquipmentModule.Entity;
using EquipmentModule.ViewModel;
using Microsoft.AspNetCore.Server.IISIntegration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EquipmentModule.Services
{
    public class SectionService : ISectionService
    {
        public void Add(SectionViewModel4Add info, string dbStr = null)
        {
            //从Base库获取数据
            dbStr = null;
            Section section = new Section();
            section.Id = Guid.NewGuid();
            section.Name = info.SectionName;
            section.ParentId = info.ParentId;
            if (info.SectionPosition!=null && info.SectionPosition.Length>1)
            {
                section.X = info.SectionPosition[0];
                section.Y = info.SectionPosition[1];
            }
            using var db = EquipmentModule.ConnCache.GetConn(dbStr);
            var list= db.Query<Section>("SELECT * FROM Md_Section WHERE Id=@id", new { id = info.ParentId }).ToList();
            if (list.Count>0)
            {
                section.Level = list[0].Level + 1;
            }
            else
            {
                section.ParentId = Guid.Empty;
                section.Level = 0;
            }
            db.Execute("INSERT INTO Md_Section(ID,ParentId,Name,Level,X,Y,CreateTime) VALUES(@Id,@ParentId,@Name,@Level,@X,@Y,GETDATE())", section);
        }

        public void Del(Guid id, string dbStr = null)
        {
            //从Base库获取数据
            dbStr = null;
            using var db = EquipmentModule.ConnCache.GetConn(dbStr);
            StringBuilder sql = new StringBuilder();
            sql.AppendLine("WITH  CTE AS( ");
            sql.AppendLine("    SELECT Id,Name,ParentId,Level,X,Y,CreateTime FROM Md_Section WHERE Id=@id");
            sql.AppendLine("    UNION ALL SELECT T.Id,T.Name,T.ParentId,T.Level,T.X,T.Y,T.CreateTime FROM Md_Section AS t");
            sql.AppendLine("    JOIN CTE AS a ON T.ParentId = A.Id");
            sql.AppendLine(")");
            sql.AppendLine("SELECT Id FROM CTE ORDER BY Level;");
            var ids = db.Query<Guid>(sql.ToString(), new { id }).ToList();
            if (ids.Count>0)
            {
                db.Execute("DELETE FROM Md_Section WHERE Id IN @ids", new { ids });
            }
        }

        public IEnumerable<Section> Index(string dbStr = null)
        {
            //从Base库获取数据
            dbStr = null;
            using var db = EquipmentModule.ConnCache.GetConn(dbStr);
            return db.Query<Section>("SELECT * FROM Md_Section ORDER BY Level DESC");
        }

        public Section Index(Guid id, string dbStr)
        {
            //从Base库获取数据
            dbStr = null;
            using var db = EquipmentModule.ConnCache.GetConn(dbStr);
            return db.Query<Section>("SELECT * FROM Md_Section WHERE Id=@id",new { id}).FirstOrDefault();
        }

        public IEnumerable<Section> IndexEquipment(string dbStr = null)
        {
            using var db = EquipmentModule.ConnCache.GetConn(dbStr);
            List<Section> sections = new List<Section>();
            var equipments= db.Query<Equipment>("SELECT Id,DevId,Name,SectionId,MachineType,X,Y FROM Md_Equipment");
            foreach (var item in equipments)
            {
                Section section = new Section()
                {
                    Id = item.Id,
                    Name = item.Name,
                    ParentId = item.SectionId,
                    X = item.X,
                    Y=item.Y,
                    Level=100,
                };
                if (item.MachineType==Util.MachineType4流向仪)
                {
                    var direction= db.ExecuteScalar<string>("SELECT TOP(1) Value FROM Hd_EquipmentHistory WHERE DevId=@DevId AND TagName='流向' ORDER BY CreateTime DESC", new { item.DevId });
                    if (!string.IsNullOrEmpty(direction))
                    {
                        section.MarkIcon = direction;
                    }
                }
                sections.Add(section);
            }
            return sections;
        }
    }
}
